﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml;
using IntraVision.Xml;

namespace IntraVision.Web
{
    public class ColumnInfo
    {
        public static int CompareColumns(ColumnInfo x, ColumnInfo y)
        {
            return x.Position - y.Position;
        }

        private string _Id;
        private int _Position;
        private bool _LoadAlways;
        private string _ValueFormat;

        public string ValueFormat
        {
            get { return _ValueFormat; }
            set { _ValueFormat = value; }
        }
        private string _Format;

        public string Format
        {
            get { return _Format; }
            set { _Format = value; }
        }

        public bool LoadAlways
        {
            get { return _LoadAlways; }
            set { _LoadAlways = value; }
        }

        public int Position
        {
            get { return _Position; }
            set { _Position = value; }
        }

        public string Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
        private string _Title;

        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        private string _SelectExpression;

        public string SelectExpression
        {
            get { return _SelectExpression; }
            set { _SelectExpression = value; }
        }

        private bool _Hidden;

        public bool Hidden
        {
            get { return _Hidden; }
            set { _Hidden = value; }
        }

        public ColumnInfo(string id, string title, string expression)
        {
            _Id = id;
            _Title = title;
            _SelectExpression = expression;
        }

        public ColumnInfo(string id, string title) : this(id, title, id) { }

        public ColumnInfo(XmlElement node)
        {
            if (string.IsNullOrEmpty(XmlUtils.AttributeValue(node, "id"))) throw new ArgumentException("Атрибут Id для колонки является обязательным.");
            _Id = node.AttributeValue("id");
            _Title = node.NodeValue("title", _Id);
            _SelectExpression = node.NodeValue("select", _Id);
            if (node.Attributes["load-always"] != null) _LoadAlways = true;
            _ValueFormat = node.NodeValue("value-format");
            _Format = node.NodeValue("format");
        }

        public void AppendHeaderXml(XmlElement header)
        {
            XmlElement xc = header.AppendElement("column");
            xc.AppendAttribute("id", _Id);
            if (_Hidden) xc.AppendAttribute("hidden", true);
            xc.AppendAttribute("position", _Position);
            xc.AppendElement("title", _Title);
        }

        public string GetValue(object entity)
        {
            Type T = entity.GetType();
            object val = T.GetProperty(Id).GetValue(entity, null);
            if (val == null) return string.Empty;
            if(string.IsNullOrEmpty(ValueFormat))
                return Convert.ToString(val, CultureInfo.InvariantCulture);
            return String.Format(CultureInfo.InvariantCulture, ValueFormat, val);
        }
    }

}
